<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>跳转测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .test-section {
            margin: 20px 0;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        .admin-btn {
            background: #ff6b35;
        }
        .admin-btn:hover {
            background: #e55a2b;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
            font-family: monospace;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔧 跳转测试页面</h1>
        
        <div class="test-section">
            <h3>📊 当前状态</h3>
            <div id="status">
                <p><strong>用户Token:</strong> <span id="user-token">无</span></p>
                <p><strong>管理员Token:</strong> <span id="admin-token">无</span></p>
                <p><strong>用户类型:</strong> <span id="user-type">无</span></p>
            </div>
        </div>

        <div class="test-section">
            <h3>🔗 直接跳转测试</h3>
            <button onclick="goToFrontend()">跳转到前端首页</button>
            <button onclick="goToBackend()" class="admin-btn">跳转到后端管理后台</button>
            <button onclick="goToLogin()">跳转到登录页</button>
        </div>

        <div class="test-section">
            <h3>👤 用户登录测试</h3>
            <input type="email" id="user-email" placeholder="用户邮箱" value="test@example.com" style="width: 200px; padding: 5px; margin: 5px;">
            <input type="password" id="user-password" placeholder="用户密码" value="password" style="width: 200px; padding: 5px; margin: 5px;">
            <button onclick="testUserLogin()">测试用户登录</button>
            <div id="user-result" class="result" style="display: none;"></div>
        </div>

        <div class="test-section">
            <h3>👨‍💼 管理员登录测试</h3>
            <input type="text" id="admin-username" placeholder="管理员用户名" value="admin@example.com" style="width: 200px; padding: 5px; margin: 5px;">
            <input type="password" id="admin-password" placeholder="管理员密码" value="admin123" style="width: 200px; padding: 5px; margin: 5px;">
            <button onclick="testAdminLogin()" class="admin-btn">测试管理员登录</button>
            <div id="admin-result" class="result" style="display: none;"></div>
        </div>

        <div class="test-section">
            <h3>🧹 清理操作</h3>
            <button onclick="clearAll()" style="background: #dc3545;">清除所有认证信息</button>
            <button onclick="refreshPage()">刷新页面</button>
        </div>
    </div>

    <script>
        // 更新状态显示
        function updateStatus() {
            document.getElementById('user-token').textContent = localStorage.getItem('token') || '无';
            document.getElementById('admin-token').textContent = localStorage.getItem('adminToken') || '无';
            document.getElementById('user-type').textContent = localStorage.getItem('userType') || '无';
        }

        // 直接跳转测试
        function goToFrontend() {
            window.location.href = 'http://127.0.0.1:5173/home';
        }

        function goToBackend() {
            window.location.href = 'http://127.0.0.1:8000/admin/dashboard';
        }

        function goToLogin() {
            window.location.href = 'http://127.0.0.1:5173/login';
        }

        // 用户登录测试
        async function testUserLogin() {
            const email = document.getElementById('user-email').value;
            const password = document.getElementById('user-password').value;
            const resultDiv = document.getElementById('user-result');
            
            resultDiv.style.display = 'block';
            resultDiv.textContent = '正在登录...';

            try {
                const response = await fetch('http://127.0.0.1:8000/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        email: email,
                        password: password
                    })
                });

                const data = await response.json();
                
                if (data.success) {
                    localStorage.setItem('token', data.data.token);
                    localStorage.setItem('user', JSON.stringify(data.data.user));
                    localStorage.setItem('userType', 'user');
                    
                    resultDiv.textContent = `✅ 用户登录成功！\nToken: ${data.data.token}\n用户: ${data.data.user.name}\n\n2秒后跳转到前端首页...`;
                    updateStatus();
                    
                    setTimeout(() => {
                        window.location.href = 'http://127.0.0.1:5173/home';
                    }, 2000);
                } else {
                    resultDiv.textContent = `❌ 用户登录失败: ${data.message}`;
                }
            } catch (error) {
                resultDiv.textContent = `❌ 用户登录错误: ${error.message}`;
            }
        }

        // 管理员登录测试
        async function testAdminLogin() {
            const username = document.getElementById('admin-username').value;
            const password = document.getElementById('admin-password').value;
            const resultDiv = document.getElementById('admin-result');
            
            resultDiv.style.display = 'block';
            resultDiv.textContent = '正在登录...';

            try {
                const response = await fetch('http://127.0.0.1:8000/api/admin/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });

                const data = await response.json();
                
                if (data.success) {
                    localStorage.setItem('adminToken', data.data.token);
                    localStorage.setItem('adminUser', JSON.stringify(data.data.user));
                    localStorage.setItem('userType', 'admin');
                    
                    resultDiv.textContent = `✅ 管理员登录成功！\nToken: ${data.data.token}\n管理员: ${data.data.user.name}\n\n2秒后跳转到后端管理后台...`;
                    updateStatus();
                    
                    setTimeout(() => {
                        window.location.href = 'http://127.0.0.1:8000/admin/dashboard';
                    }, 2000);
                } else {
                    resultDiv.textContent = `❌ 管理员登录失败: ${data.message}`;
                }
            } catch (error) {
                resultDiv.textContent = `❌ 管理员登录错误: ${error.message}`;
            }
        }

        // 清除所有认证信息
        function clearAll() {
            localStorage.removeItem('token');
            localStorage.removeItem('adminToken');
            localStorage.removeItem('user');
            localStorage.removeItem('adminUser');
            localStorage.removeItem('userType');
            updateStatus();
            
            document.getElementById('user-result').style.display = 'none';
            document.getElementById('admin-result').style.display = 'none';
            
            alert('已清除所有认证信息');
        }

        // 刷新页面
        function refreshPage() {
            window.location.reload();
        }

        // 页面加载时更新状态
        window.onload = function() {
            updateStatus();
        };
    </script>
</body>
</html>
